{% extends "dissemin/details.html" %}

{% load i18n %}


{% block extra_head %}
{% include 'statistics/headers.html' %}
{% endblock %}

{% block headTitle %}{{ department.name }}{% endblock %}

{% block bodyTitle %}<span id="department_name" data-params="{csrfmiddlewaretoken:'{{csrf_token}}'}">{{ department.name }}</span>

    {% endblock %}

{% block jsScript %}
{% if request.user.is_superuser %}
$(function(){
    makeTextEditable($('#department_name'), '/ajax/change-department', '{{ department.pk }}', 'name');
});

function addResearcher() {
    var data = $('#addResearcherForm').serialize();
    $('#addResearcherForm .formErrorDiv').each(function () {
        $(this).text('');
        });

   $.post('{% url 'ajax-addResearcher' %}', data, null, 'json').fail(function(data) {
            if(!data.responseJSON) {
                {% trans "Dissemin encountered an error, please try again later." as errormsg %}
                $('#globalError').text('{{ errormsg|escapejs }}');
            } else {
                error = data.responseJSON;
                for(field in error) {
                    if(field == 'message') {
                        $('#globalError').text(error[field]);
                    } else {
                        $('#error-'+field).text(error[field]);
                    }
                }
            }
        }).done(function(data) {
        $('#membersList').append('<li><a href="/researcher/'+data["id"]+'/">'+data['name']+'</a></li>');
        $('#addResearcherForm .form-control').trigger("reset");
    });
}
{% endif %}
{% endblock %}

{% block lists %}
<p></p>
<ul id="membersList" class="noCircle">
    {% for person in department.sorted_researchers %}
    {% ifchanged person.name.first_letter %}
    <li class="membersLetter">{{ person.name.first_letter }}</li>
    {% endifchanged %}
    <li><a href="{{ person.url }}">{{ person }}</a>
           {% if person.stats %}
           <span class="numPapers">
               {% blocktrans trimmed count num_total=person.stats.num_tot %}
               ({{ num_total }} paper)
               {% plural %}
               ({{ num_total }} papers)
               {% endblocktrans %}
           </span>
           {% endif %}

        {% if request.user.is_superuser %}
            <button type="button" class="close researcherDeleteButton"
                id="delete-researcher-{{ person.id }}"
                data-undotext="{% trans "Researcher deleted. " %}"
                data-undoundo=""
                data-undolink="#"
                data-undoclass="">
                <span aria-hidden="true">&times;</span><span class="sr-only">{% trans "Delete" %}</span>
            </button>
        {% endif %}

    </li>
    {% endfor %}
</ul>

{% endblock %}

{% block details %}
<span class="detailsTitle">{% trans "Department" %}</span>
<div class="detailsContent">
    <p><a href="{% url 'department-papers' pk=department.id %}">{% trans "Papers of the members" %}</a></p>

    {% include "statistics/pie.html" with obj=department stats_counter=1 %}
</div>
{% endblock %}

